//
//  AppDelegate.swift
//  WB123
//
//  Created by xuyunde mac pro on 2017/6/29.
//  Copyright © 2017年 NSObjiect. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
        
        // 实例化
        window = UIWindow(frame:UIScreen.main.bounds)
        //背景
        window?.backgroundColor = UIColor.white
        //根控制器
        setupRootViewController()
        window?.makeKeyAndVisible()
       
        print(WBOAuthViewModel.share.isLogin)
        // 注册通知  更换根控制器
        NotificationCenter.default.addObserver(self, selector: #selector(switchRootViewController), name: NSNotification.Name(rawValue: "HMSWITCHROOTVIEWCONTROLLERNOTI"), object: nil)
        return true
    }
    // 监听通知切换根控制器
    @objc private func switchRootViewController(noti: Notification){
     
        // 如果object->不为nil >WBMainViewController
        if let _ = noti.object {
            
            window?.rootViewController = WBMainViewController()
        }else{
            // 如果object->为nil >wbwelcomeViewController
            window?.rootViewController = WBWelcomeViewController()
        }
    }
   // 通过用户是否登录来设置控制器
    private func setupRootViewController(){
        
        if WBOAuthViewModel.share.isLogin {
            window?.rootViewController = WBWelcomeViewController()
        }else{
            window?.rootViewController = WBMainViewController()
        }
    }

}

